﻿using ELab.Serialization;
using ELab.Serialization.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ELab.Communication.Messages.ControllerMessages
{
    public class GetUsersResponseMessage : ControlMessage
    {
        /// <summary>
        /// Gets MessageTypeId for GetApplicationListResponseMessage.
        /// </summary>
        public override int MessageTypeId
        {
            get { return ControlMessageFactory.MessageTypeIdGetUsersResponseMessage; }
        }

        public UserResponse[] Users { get; set; }

        public override void Serialize(IELabSerializer serializer)
        {
            base.Serialize(serializer);
            serializer.WriteObjectArray(Users);
        }

        public override void Deserialize(IELabDeserializer deserializer)
        {
            base.Deserialize(deserializer);
            Users = deserializer.ReadObjectArray(() => new UserResponse());
        }

        public class UserResponse : User
        {
            public UserResponse() : base() { }

            public UserResponse(User user)
            {
                if (user != null)
                {
                    this.ApplicationName = user.ApplicationName;
                    this.Avatar = user.Avatar;
                    this.Email = user.Email;
                    this.Fullname = user.Fullname;
                    this.ID = user.ID;
                    this.IP = user.IP;
                    this.Password = user.Password;
                    this.RemotePassword = user.RemotePassword;
                    this.RemoteUser = user.RemoteUser;
                    this.Surveys = user.Surveys;
                    this.Type = user.Type;
                    this.Surveys = user.Surveys;
                    this.UserName = user.UserName;
                }
            }

            public bool IsOnline { get; set; }

            public override void Serialize(IELabSerializer serializer)
            {
                serializer.WriteStringUTF8(ApplicationName);
                serializer.WriteStringUTF8(ID.ToString());
                serializer.WriteStringUTF8(Type.ToString());
                serializer.WriteStringUTF8(UserName);
                serializer.WriteStringUTF8(Password);
                serializer.WriteStringUTF8(Email);
                serializer.WriteStringUTF8(Fullname);
                serializer.WriteStringUTF8(IP);
                serializer.WriteStringUTF8(RemoteUser);
                serializer.WriteStringUTF8(RemotePassword);
                serializer.WriteByteArray(Avatar);
                serializer.WriteObjectArray(Surveys);
                serializer.WriteBoolean(this.IsOnline);
            }

            public override void Deserialize(IELabDeserializer deserializer)
            {
                this.ApplicationName = deserializer.ReadStringUTF8();
                this.ID = new Guid(deserializer.ReadStringUTF8());
                UserType userType;
                Enum.TryParse<UserType>(deserializer.ReadStringUTF8(), out userType);
                this.Type = userType;
                this.UserName = deserializer.ReadStringUTF8();
                this.Password = deserializer.ReadStringUTF8();
                this.Email = deserializer.ReadStringUTF8();
                this.Fullname = deserializer.ReadStringUTF8();
                this.IP = deserializer.ReadStringUTF8();
                this.RemoteUser = deserializer.ReadStringUTF8();
                this.RemotePassword = deserializer.ReadStringUTF8();
                this.Avatar = deserializer.ReadByteArray();
                this.Surveys = deserializer.ReadObjectArray(() => new Survey());
                this.IsOnline = deserializer.ReadBoolean();
            }
        }
    }
}